<template>
  <div>
    <!-- 添加模态框开始 -->
    <el-dialog title="添加汽车" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="车牌号" :label-width="formLabelWidth" prop="cCard">
          <el-input
            v-model="ruleForm.cCard"
            autocomplete="off"
            style="width: 550px"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" prop="cSize" :label-width="formLabelWidth">
          <el-select
            v-model="ruleForm.cSize"
            placeholder="请选择车辆类型"
            style="width: 550px"
          >
            <el-option label="大型" value="大"></el-option>
            <el-option label="中型" value="中"></el-option>
            <el-option label="小型" value="小"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">添加</el-button>
      </div>
    </el-dialog>
    <!-- 添加模态框结束 -->

<!-- 修改模态框开始 -->
    <el-dialog title="添加汽车" :visible.sync="dialogFormVisible2">
      <el-form
        :model="ruleForm2"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="车牌号" :label-width="formLabelWidth" prop="">
          <el-input
            v-model="ruleForm2.cCard"
            autocomplete="off"
            style="width: 440px"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" prop="" :label-width="formLabelWidth">
          <el-select
            v-model="ruleForm2.cSize"
            placeholder="请选择车辆类型"
            style="width: 440px"
          >
            <el-option label="大" value="大"></el-option>
            <el-option label="中" value="中"></el-option>
            <el-option label="小" value="小"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">修改</el-button>
      </div>
    </el-dialog>
    <!-- 修改模态框结束 -->


    <el-button type="text" @click="updateaddUI" style="float: right"
      >增加绑定车辆</el-button
    >

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="cId" label="汽车id" width="300"> </el-table-column>
      <el-table-column prop="cCard" label="车牌号" width="300">
      </el-table-column>
      <el-table-column prop="cSize" label="类型"> </el-table-column>

      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="updateUI(scope.row)" round
            >修改</el-button
          >
          <el-button type="danger" @click="open(scope.row)" round>删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
  


<script>
export default {
  data() {
    return {
      ruleForm: {
        cSize: "",
        cCard: "",
        uId: "",
      },
       ruleForm2: {
        cSize: "",
        cCard: "",
        uId: "",
      },
      tableData: [],
      dialogFormVisible: false,
       dialogFormVisible2: false,
      formLabelWidth: "120px",
      rules: {
        cCard:[
          {required:true,message:"请填写车牌号",trigger:"blur"},
          {pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,message:'请输入正确格式的车牌号'},

        ],
        cSize: [
          { required: true, message: "请选择车辆类型", trigger: "change" },
        ],
      },
    };
  },

  methods: {

    update(){
      console.log(this.ruleForm2);
      this.$axios.post("car/update",this.ruleForm2).then(res=>{
        if(res.data.code == 200){
          this.$message.success("修改成功");
          this.dialogFormVisible2=false;
          this.ruleForm2.resetFields;
        }
      });
    },

    updateaddUI() {
      let u=JSON.parse(sessionStorage.getItem("u"));
      let uId =u.uId;
      this.dialogFormVisible = true;
      this.ruleForm.uId = uId;
    },
    updateUI(row) {
      this.dialogFormVisible2=true
      this.ruleForm2=row;
    },
    save() {

      if (this.tableData.length > 4) {
        this.$message.warning("最多绑定5辆汽车");
        this.dialogFormVisible = false;
         this.$refs.ruleForm.resetFields();
      } else {
        this.$axios.post("car/save", this.ruleForm).then((res) => {
          if (res.data.code == 200) {
            this.$message.success("添加车辆成功");
            this.dialogFormVisible = false;
            this.$refs.ruleForm.resetFields();
            this.load();
          }
        });
      }
    },
    load() {
       let u=JSON.parse(sessionStorage.getItem("u"));
      let uId =u.uId;
      let config = {
        headers: {
          "Content-Type": "text/plain",
        },
      };
      this.$axios.post("car/findUWC", uId, config).then((res) => {
        this.tableData = res.data.data;
      });
    },
    open(row) {
      this.$confirm("确定删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let cId = row.cId;
          let config = {
            headers: {
              "Content-Type": "text/plain",
            },
          };
          console.log(cId);
          this.$axios.post("car/delete", cId, config).then((res) => {
            this.$message.success("删除车辆成功");
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted: function () {
    this.load();
  },
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>